#!/bin/bash

# 获取外部传入的app_name参数
app_name="$1"

# 模板目录路径
template_dir="./react-app"

# 检查app_name参数是否传入
if [ -z "$app_name" ]; then
    echo "Error: No app_name provided."
    echo "Usage: $0 app_name"
    exit 1
fi

# 检查app_name是否已经存在
if [ -d "$app_name" ]; then
    echo "Error: Directory '$app_name' already exists."
    exit 1
fi

# 创建新的app目录
mkdir "$app_name"

# 检查目录是否创建成功
if [ $? -ne 0 ]; then
    echo "Error: Failed to create directory '$app_name'."
    exit 1
fi

# 拷贝模板目录中的文件到新的app目录，排除node_modules
rsync -a --info=progress2 "$template_dir/" "$app_name/" --exclude node_modules

# 检查拷贝是否成功
if [ $? -ne 0 ]; then
    echo "Error: Failed to copy files from '$template_dir' to '$app_name'."
    exit 1
fi

# 安装 node_modules
cd "$app_name"
yarn install
cd -

# ignore dist for app
echo "/dist" >> $app_name/.gitignore
echo "App '$app_name' has been created successfully."

